<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
      <el-form-item label="材料编号" prop="materialsNo">
        <el-input
          v-model="queryParams.materialsNo"
          placeholder="请输入材料编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="材料名称" prop="materialsName">
        <el-input
          v-model="queryParams.materialsName"
          placeholder="请输入材料名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="材料类别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入材料类别"
          clearable
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['sources:materials:add']"
          v-preventReClick
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="materialsList">
      <el-table-column label="主键id" align="center" prop="materialsId" />
      <el-table-column label="材料编号" align="center" prop="materialsNo" />
      <el-table-column label="材料名称" align="center" prop="materialsName" />
      <el-table-column label="材料类别" align="center" prop="category" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="采购价格" align="center" prop="puchasePrice" />
      <el-table-column label="库存数量" align="center" prop="stock" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['sources:materials:edit']"
            v-preventReClick
          >修改</el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['sources:materials:remove']"
            v-preventReClick
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
    />

    <!-- 添加或修改鉴定材料对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="材料编号" prop="materialsNo">
          <el-input v-model="form.materialsNo" placeholder="请输入材料编号" />
        </el-form-item>
        <el-form-item label="材料名称" prop="materialsName">
          <el-input v-model="form.materialsName" placeholder="请输入材料名称" />
        </el-form-item>
        <el-form-item label="材料类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入材料类别" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="采购价格" prop="puchasePrice">
          <el-input v-model="form.puchasePrice" placeholder="请输入采购价格" />
        </el-form-item>
        <el-form-item label="库存数量" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import Materials from '@/views/sources/materials/index.vue'
import { MaterialsService } from '@/api/sources/materials.js'

export default {
  name: "Materials",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 鉴定材料表格数据
      materialsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialsNo: null,
        materialsName: null,
        category: null,
        supplier: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialsNo: [
          { required: true, message: "材料编号不能为空", trigger: "blur" }
        ],
        materialsName: [
          { required: true, message: "材料名称不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "材料类别不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        supplier: [
          { required: true, message: "供应商不能为空", trigger: "blur" }
        ],
        puchasePrice: [
          { required: true, message: "采购价格不能为空", trigger: "blur" }
        ],
        stock: [
          { required: true, message: "库存数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询鉴定材料列表 */
    getList() {
      this.loading = true;
      MaterialsService.listMaterials(this.queryParams).then(response => {
        this.materialsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialsId: null,
        materialsNo: null,
        materialsName: null,
        category: null,
        unit: null,
        supplier: null,
        puchasePrice: null,
        stock: null,
        remark: null,
        createTime: null,
        updateTime: null
      };
      this.$nextTick(()=>{
        this.$refs.form.resetFields()
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加鉴定材料";
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialsId = row.materialsId || this.ids
      MaterialsService.getMaterials(materialsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改鉴定材料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialsId != null) {
            MaterialsService.updateMaterials(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            MaterialsService.addMaterials(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialsIds = row.materialsId || this.ids;
      this.$confirm('是否确认删除鉴定材料编号为"' + materialsIds + '"的数据项？').then(function() {
        return MaterialsService.delMaterials(materialsIds);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
